/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/1
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.Controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it1997.it1997admin.pojo.User;
import com.it1997.it1997admin.service.RoleService;
import com.it1997.it1997admin.service.UserService;
import com.it1997.it1997admin.utils.ResultUtil;
import com.it1997.it1997admin.vo.AdminVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.StrokeStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/admin")
@Api(tags= "后台处理类")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @GetMapping("/getCaptcha")
    @ResponseBody
    @ApiOperation(value = "获取验证码")
    public String getCaptcha(HttpServletResponse response){
        LineCaptcha lineCaptcha= CaptchaUtil.createLineCaptcha(200,100);
        //lineCaptcha.write("E:/line.png");
        String img=lineCaptcha.getImageBase64();
        String captchaCode=lineCaptcha.getCode();
        Map<String,Object> map=new HashMap<>();
        map.put("captcha","data:image/png;base64,"+img);
        map.put("verify",captchaCode);
        return ResultUtil.success(1,"成功",map);
    }
    @GetMapping("/getUserList")
    @ResponseBody
    @ApiOperation(value = "获取管理员列表")
    public String getAdminList(Integer page,Integer limit){
        List<AdminVo> adminVoList=userService.getAdminList(page,limit);
        Integer adminCount=userService.getAdminCount();
        if(adminVoList.size()>0){
            return ResultUtil.tableData(0,"成功",adminCount,adminVoList);
        }
        return ResultUtil.fail(1,"没有查询到任何数据！",adminVoList);
    }
    @GetMapping("/getUserInfo")
    @ResponseBody
    @ApiOperation(value = "获取登录用户信息")
    public String getUserInfo(@AuthenticationPrincipal User user){
        return JSON.toJSONString(user);
    }
    @PostMapping("/addUser")
    @ResponseBody
    @ApiOperation(value = "添加管理员账号")
    @Transactional
    public String addAdmin(@RequestBody AdminVo adminVo){
        log.info(String.valueOf(adminVo));
        int i= userService.addAdmin(adminVo);
        if(i==1){
            return ResultUtil.success(1,"添加成功");
            }
        return ResultUtil.fail(0,"添加失败，用户已存在");

    }
    @GetMapping("/getAdminById")
    @ResponseBody
    @ApiOperation(value = "根据用户id获取用户信息")
    public String getAdminById(@RequestParam("userId") Integer userId){
        Integer roleId=roleService.getRoleById(userId);
    AdminVo adminVo= userService.getAdminById(userId);
        adminVo.setRoleId(roleId);
        if(adminVo!=null)
            return ResultUtil.success(1,"查询成功",adminVo);
        else
                return ResultUtil.fail(0,"查询失败",adminVo);
}
    @PostMapping("/updateAdminById")
    @ResponseBody
    @ApiOperation(value = "根据用户id更新用户信息")
    public String updateAdminById(@RequestBody AdminVo adminVo){
        Integer i=userService.updateAdminById(adminVo);
        log.info(String.valueOf(adminVo));
        if(i==0){
            return ResultUtil.fail(0,"修改失败");
        }else {
            return ResultUtil.success(1,"修改成功");
        }
    }
    @PostMapping("/deleteAdmin")
    @ResponseBody
    @ApiOperation(value = "删除管理员用户")
    public String deleteAdminById(@RequestBody List<AdminVo> adminVoList){
        Integer i=userService.deleteAdminById(adminVoList);
        log.info(String.valueOf(adminVoList));
        if(i!=0){
            return ResultUtil.success(1,"删除成功");
        }else {
            return ResultUtil.fail(0,"删除失败");
        }
    }
    @PostMapping("/searchAdmin")
    @ResponseBody
    @ApiOperation(value = "搜索管理员用户")
    public String searchAdmin(@RequestBody AdminVo adminVo){
        /*log.info(String.valueOf(data));
        Object datas=JSON.toJSON(data);*/

        Integer adminCount=userService.getSearchAdminCount(adminVo);
        List<AdminVo> adminVoList=userService.searchAdmin(adminVo);
        log.info(adminVoList.toString());
        if(adminVoList.size()>0){
            return ResultUtil.tableData(0,"查询成功",adminCount,adminVoList);
        }else {
            return ResultUtil.fail(1,"暂无数据");
        }
    }
}
